package com.shwread.android.qysw.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import com.shwread.android.qysw10000182.R;
import com.tgx.tina.android.task.ATaskService;
import java.util.ArrayList;
import java.util.List;
import logic.action.GetAllBookNoteAction;
import logic.action.GetAllBookNoteAction.AllBookNotesListener;
import logic.bean.BookBean;
import logic.bean.MyNoteInfo;
import logic.constants.Const;
import logic.constants.DefaultConsts;
import logic.dao.BookDao;
import logic.util.Util;


/**
 * @Project： Lqyd_Android
 * @Title：BookNotesPage
 * @Description：我的笔记
 * @Author：zhangnannan
 * @Date：2014年8月21日 上午10:42:19
 */
public class BookNotesActivity
        extends
        BaseBusinessActivity
        implements
        OnClickListener,
        AllBookNotesListener
{
	
	private Context                                   context;
	private Button                                    btnBack;
	private TextView                                  tvTitle;
	private ListView                                  lvNotesInfo;
	private NotesInfoAdapter                          notesInfoAdapter;
	private RelativeLayout                            emptyView;
	
	public GetAllBookNoteAction<BaseBusinessActivity> getAllBookNoteAction;
	
	private List<MyNoteInfo>                          bookNoteList;
	
	private Handler                                   myHandler = new Handler()
	                                                            {
		                                                            
		                                                            @Override
		                                                            public void handleMessage(Message msg) {
			                                                            switch (msg.what) {
																			case DefaultConsts.UPDATEUI_ALL_BOOK_NOTE:
																				if (notesInfoAdapter.getCount() <= 0)
																				{
																					lvNotesInfo.setVisibility(View.GONE);
																					emptyView.setVisibility(View.VISIBLE);
																					return;
																				}
																				else
																				{
																					lvNotesInfo.setVisibility(View.VISIBLE);
																					emptyView.setVisibility(View.GONE);
																					notesInfoAdapter.notifyDataSetChanged();
																				}
																				
																			default:
																				break;
																		}
																	}
																	
	                                                            };
	
	@Override
	public Handler getUIHandler() {
		return myHandler;
	}
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.book_notes);
		context = this;
		mAService = new ATaskService();
		mAService.startAService(context);
		initView();
		setListener();
		initConst();
		initData();
	}
	
	@Override
	protected void onResume() {
		getAllBookNoteAction.start(null);
		super.onResume();
	}
	
	@Override
	protected void onDestroy() {
		if (null != mAService) mAService.stopAService();
		super.onDestroy();
	}
	
	void initConst() {
		getAllBookNoteAction = new GetAllBookNoteAction<BaseBusinessActivity>(this);
		getAllBookNoteAction.OnSearchInfoListener(this);
	}
	
	private void setListener() {
		btnBack.setOnClickListener(this);
		
	}
	
	private void initData() {
		bookNoteList = new ArrayList<MyNoteInfo>();
		notesInfoAdapter = new NotesInfoAdapter();
		lvNotesInfo.setAdapter(notesInfoAdapter);
		
		lvNotesInfo.setOnItemClickListener(new OnItemClickListener()
		{
			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
				MyNoteInfo info = bookNoteList.get(arg2);
				BookBean bean = getBookInfo(info.getBook_db_id(), info.getContentId());
				startIntent(info, bean);
			}
		});
	}
	
	void startIntent(MyNoteInfo info, BookBean bean) {
		Intent intent = new Intent(this, BookNotesDetailActivity.class);
		intent.putExtra(DefaultConsts.db_id_s, String.valueOf(info.getBook_db_id()));
		intent.putExtra(DefaultConsts.book_contentId_s, String.valueOf(bean.book_id));
		intent.putExtra(DefaultConsts.book_name, bean.book_name);
		startActivity(intent);
	}
	
	private void initView() {
		
		tvTitle = (TextView) findViewById(R.id.common_header_name);
		tvTitle.setText("我的笔记");
		btnBack = (Button) findViewById(R.id.common_header_back);
		btnBack.setVisibility(View.VISIBLE);
		
		lvNotesInfo = (ListView) findViewById(R.id.book_notes_bookinfo_list_lv);
		emptyView = (RelativeLayout) findViewById(R.id.book_notes_list_empty_rlay);
	}
	
	@Override
	public void onClick(View v) {
		switch (v.getId()) {
			case R.id.common_header_back:
				finish();
				break;
			default:
				break;
		
		}
		
	}
	
	private class NotesInfoAdapter
	        extends
	        BaseAdapter
	{
		
		ViewHolder holder;
		
		@Override
		public int getCount() {
			if (bookNoteList != null) return bookNoteList.size();
			return 0;
		}
		
		@Override
		public Object getItem(int position) {
			return bookNoteList.get(position);
		}
		
		@Override
		public long getItemId(int position) {
			return position;
		}
		
		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			if (convertView == null)
			{
				holder = new ViewHolder();
				convertView = getLayoutInflater().inflate(R.layout.book_notes_bookinfo_list_item, null);
				
				holder.ivCover = (ImageView) convertView.findViewById(R.id.book_notes_bookinfo_list_item_cover_iv);
				holder.tvName = (TextView) convertView.findViewById(R.id.book_notes_bookinfo_list_item_name_tv);
				holder.tvAuthor = (TextView) convertView.findViewById(R.id.book_notes_bookinfo_list_item_author_tv);
				holder.tvCount = (TextView) convertView.findViewById(R.id.book_notes_bookinfo_list_item_count_tv);
				convertView.setTag(holder);
			}
			else
			{
				holder = (ViewHolder) convertView.getTag();
			}
			
			MyNoteInfo bean = bookNoteList.get(position);
			
			BookBean book = getBookInfo(bean.getBook_db_id(), bean.getContentId());
			
			if (Util.isNotEmpty(book.logo_url))
			{
				Util.loadImg(context, bean.getContentId(), book.logo_url, holder.ivCover);
			}
			else
			{
				holder.ivCover.setImageDrawable(getResources().getDrawable(R.drawable.bookcover_default));
			}
			holder.tvName.setText(book.book_name);
			holder.tvAuthor.setText(getResources().getString(R.string.book_author, Util.isEmpty(book.author) ? "佚名" : book.author));
			holder.tvCount.setText(getResources().getString(R.string.note_count, String.valueOf(bean.getNoteCount())));
			
			return convertView;
		}
		
	}
	
	class ViewHolder
	{
		ImageView ivCover;
		TextView  tvAuthor, tvName;
		TextView  tvCount;
	}
	
	@Override
	public void setAllBookInfos(List<MyNoteInfo> infos) {
		if(bookNoteList.size()>0)bookNoteList.removeAll(bookNoteList);
		bookNoteList = infos;
		//emptyView.setVisibility((bookNoteList.size() < 1) ? View.VISIBLE : View.GONE);
	}
	
	private BookBean getBookInfo(long dbId, String contentId) {
		return BookDao.getInstance(context).getBookById(dbId,Long.parseLong(contentId), Const.phone_number);
	}
	
}
